package com.oymn.myapplication.page;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.oymn.myapplication.R;

public class buy_goods extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_goods);


        //设置价格
        Intent intent=getIntent();
        String s=intent.getStringExtra("com.example.myapplication.buy_goods.inputPrice");
        TextView buy_goods_price=findViewById(R.id.buy_goods_price);
        buy_goods_price.setText(s);
        TextView heji=findViewById(R.id.heji);
        heji.setText(s);
        //设置商品名称
        String s1=intent.getStringExtra("com.example.myapplication.buy_goods.inputName");
        TextView buy_goods_name=findViewById(R.id.buy_goods_name);
        buy_goods_name.setText(s1);

        //设置图片
        ImageView imageView=findViewById(R.id.buy_goods_image);
        Bitmap bitmap = intent.getParcelableExtra("com.example.myapplication.buy_goods.goods_img");
        imageView.setImageBitmap(bitmap);

        Button reduce =findViewById(R.id.reduce);
        Button increase = findViewById(R.id.increase);
        TextView goods_shuliang = findViewById(R.id.goods_shuliang);

        reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = goods_shuliang.getText().toString();
                int temp= Integer.parseInt(s1);
                temp=temp-1;
                String s2 = String.valueOf(temp);
                goods_shuliang.setText(s2);

                TextView buy_goods_price=findViewById(R.id.buy_goods_price);
                String price=buy_goods_price.getText().toString();
                double price_double = Double.parseDouble(price);
                double temp1=(double)temp;
                double account_sum=temp1*price_double;
                heji.setText(String.valueOf(account_sum));
            }
        });

        increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1=goods_shuliang.getText().toString();
                int temp=Integer.parseInt(s1);
                temp=temp+1;
                String s2=String.valueOf(temp);
                goods_shuliang.setText(s2);

                TextView heji=findViewById(R.id.heji);
                TextView buy_goods_price=findViewById(R.id.buy_goods_price);
                String price=buy_goods_price.getText().toString();
                double price_double = Double.parseDouble(price);
                double temp1=(double)temp;
                double account_sum=temp1*price_double;
                heji.setText(String.valueOf(account_sum));
            }
        });

        //返回按钮
        Button return_goods=findViewById(R.id.return_goods);
        return_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //提交订单
        Button dingdan =findViewById(R.id.dingdan);
        dingdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(getApplication(), "提交订单成功", Toast.LENGTH_SHORT).show();


                finish();

            }
        });

    }
}